import {getAgreementApprovalList, getAgreementlList, addAgreement, updateAgreement, getAgreementById, deleteAgreementById, agreementApproval, getCurrentNodeDetail} from '../../services/customer/businessagreement';
import Constants from '../../utils/Constants';
import OperatorConstant from "../../utils/OperatorConstant";
import { message } from "antd";
const R = require('ramda');

export default {
    namespace: 'businessAgreement',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        approvalPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list: [],
        approvalList: [],
        dataSource: {},
        agreementStatus: "1",
        approval: {},
        node:{},


    },
    reducers: {

        //-----------------setList

        /**
         * 设置数据列表
         * @param state
         * @param payload
         * @returns {{list, page}}
         */
        setList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page , agreementStatus : payload.status}
        },

        setApprovalList(state, { payload }) {
            return { ...state, approvalList: payload.list, approvalPage: payload.page}
        },



        //--------------------setEntity

        /**
         * 设置
         * @param state
         * @param action
         * @returns {{product: (string|state.product|{}|*)}}
         */
        setDataSource(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.dataSource,};
        },

        setApproval(state, action) {
            let payload = action.payload;
            return {...state, approval: payload.approval,};
        },

        /**
         * 工作流审核节点信息
         * @param state
         * @param action
         * @returns {{node}}
         */
        setNode(state, action) {
            let payload = action.payload;
            return {...state, node: payload.node,};
        },

        /**
         * 设置page
         * @param state
         * @param action
         * @returns {{approval: (state.approval|{}|*|string|effects.approval)}}
         */
        setPage(state, action) {
            let payload = action.payload;
            return {...state, page: payload.page,};
        },


    },
    effects: {


        /**
         * 获取采购合同信息列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getAgreementlList({payload}, { call, put, select }) {
            let page = yield select(state => state.businessAgreement.page);
                payload.page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;
            let status =  yield select(state => state.businessAgreement.agreementStatus);
                payload.status =  R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;

            const res = yield call(getAgreementlList, {...R.omit(["onSuccess", "onFail"])(payload)});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status : status,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        *getAgreementApprovalList({payload}, { call, put, select }) {
            let page = yield select(state => state.businessAgreement.approvalPage);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getAgreementApprovalList, {
                id: R.isNil(payload) || R.isNil(payload.id) ? "" : payload.id,
                page
            });


            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setApprovalList',
                    payload: {
                        list: R.isNil(content) || R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content) || R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },



        /**
         * 获取采购合同详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getAgreementById({ payload }, { call, put, select }) {
            const res = yield call(getAgreementById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setDataSource",
                    payload: {
                        dataSource: data,
                    }
                });

            }
        },


        /**
         * 删除采购合同
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteAgreementById({ payload }, { call, put, select }) {
            let page = yield select(state => state.businessAgreement.page);
            let status =  yield select(state => state.businessAgreement.agreementStatus);

            const res = yield call(deleteAgreementById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除采购合同成功");

                if(payload.onSuccess)payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加采购合同
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addAgreement({payload}, { call, put, select }) {
            let page = yield select(state => state.businessAgreement.page);
            let status =  yield select(state => state.businessAgreement.agreementStatus);

            const res = yield call(addAgreement, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("增加采购合同成功");

                if(payload.onSuccess())payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新采购合同
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updateAgreement({payload}, { call, put, select }) {
            let page = yield select(state => state.businessAgreement.page);
            let status =  yield select(state => state.businessAgreement.agreementStatus);

            const res = yield call(updateAgreement, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("修改采购合同成功");

                if(payload.onSuccess())payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 审核采购合同
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *agreementApproval({payload}, { call, put, select }) {

            const res = yield call(agreementApproval, payload);
            let page = yield select(state => state.businessAgreement.page);
            let status =  yield select(state => state.businessAgreement.agreementStatus);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("审核采购合同成功");

                if(payload.onSuccess)payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取当前工作流审核节点
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getCurrentNodeDetail({ payload }, { call, put, select }) {
            const res = yield call(getCurrentNodeDetail, {id: payload.id, code: payload.code});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setNode",
                    payload: {
                        node: data,
                    }
                });

            }
        },

    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};